<template>
  <div class="user-page">
    <div class="my-title">
      <img src="http://liufusong.top:8080/img/profile/bg.png" alt="" />
      <div class="my-info">
        <div class="my-myicon">
          <img src="../assets/avatar.png" alt="" />
        </div>
        <div class="my-user">
          <div class="my-name">{{ username }}</div>
          <div class="my-edit">
            <div class="btn" @click="add">
              {{ password }}
            </div>
          </div>
          <div class="use" v-show="isShow">编辑个人资料</div>
        </div>
      </div>
    </div>
    <div class="list">
      <van-grid :column-num="3" :border="false" icon-size="22px">
        <van-grid-item icon="star-o" text="我的收藏" border to="/enshrine" />
        <van-grid-item
          icon="wap-home-o"
          text="我的出租"
          border
          to="/houserent"
        />
        <van-grid-item icon="underway-o" text="看房记录" border />
        <van-grid-item icon="idcard" text="成为房主" border />
        <van-grid-item icon="contact" text="个人资料" border />
        <van-grid-item icon="service-o" text="联系我们" border />
      </van-grid>
    </div>
    <div class="join">
      <img src="../assets/bg1.png" alt="" />
    </div>
  </div>
</template>

<script>
import { user } from "@/api/user";
import { getToken } from "@/utils/storage";
import { removeToken } from "@/utils/storage";
export default {
  name: "user-page",
  data() {
    return {
      username: "好客",
      password: "去登陆",
      isShow: false,
    };
  },
  async created() {
    const data = await user();
    if (getToken("token")) {
      this.username = data.body.nickname;
      this.password = "退出";
      this.isShow = true;
    }
  },
  methods: {
    add() {
      if (getToken()) {
        this.$dialog
          .confirm({
            title: "提示",
            message: "是否确定退出？",
          })
          .then(() => {
            removeToken();
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      } else {
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.user-page {
  background: #fff;
  height: 100vh;
  .mt20 {
    margin-top: 20px;
  }
  .user {
    display: flex;
    padding: 20px 0;
    align-items: center;
    img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      overflow: hidden;
    }
    h3 {
      margin: 0;
      padding-left: 20px;
      font-size: 18px;
    }
  }
}
.my-title {
  min-height: 300px;
  position: relative;
  img {
    width: 100%;
  }
  .my-info {
    position: absolute;
    background: #fff;
    width: 73%;
    height: 55%;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0 0 10px 3px #ddd;
    margin: 50px auto 0;
    padding: 0 20px;
    text-align: center;
    font-size: 13px;
    .my-myicon {
      position: relative;
      transform: translateY(-50%);
      border-radius: 50%;
      width: 70px;
      height: 70px;
      border: 5px solid #f5f5f5;
      display: inline-block;
      box-shadow: 0 2px 2px #bdbdbd;
    }
    .my-user {
      .my-name {
        margin-top: -20px;
        margin-bottom: 10px;
      }
      .my-edit {
        color: #999;
        font-size: 12px;
        margin-top: 25px;
        .btn {
          position: absolute;
          left: 39%;
          width: 70px;
          height: 30px;
          background-color: red;
          line-height: 30px;
          text-align: center;
          color: #fff;
          border-radius: 5px;
        }
      }
    }
    .use {
      color: #999;
      font-size: 12px;
      margin-top: 60px;
    }
  }
}

.list {
  margin-top: 30px;
}
.join {
  display: flex;
  margin-top: 8px;
  justify-content: space-evenly;
  img {
    width: 93%;
    border-radius: 5px;
  }
}
</style>
